//
//  UITextField+PlaceHolder.m
//  runtime
//
//  Created by eport on 2020/12/13.
//

//文章参考:https://www.jianshu.com/p/e0d46032b27f

#import "UITextField+PlaceHolder.h"
#import <objc/runtime.h>

@implementation UITextField (PlaceHolder)

-(void)changePlaceHolderTextColor:(UIColor *)color{
    
    unsigned int count;
    //获取UITextField中的所有成员变量
    Ivar *ivars = class_copyIvarList([UITextField class], &count);
    for (int i = 0; i < count; i++) {
    // 取出i位置的成员变量
    Ivar ivar = ivars[i];
   // NSLog(@"成员变量%s", ivar_getName(ivar));
    }
    free(ivars);
     
    
    // iOS 13 通过 KVC 方式修改私有属性，有 Crash 风险，谨慎使用！并不是所有KVC都会Crash，要尝试！
    if ([[UIDevice currentDevice].systemVersion floatValue] > 13.0) {
        //获取一个成员变量,根据名称获取
        Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
        //object_getIvar获取成员变量的值
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = color;

    }else{
        //kvc赋值,iOS13之前用kvc赋值就可以哦
        [self setValue:color forKeyPath:@"_placeholderLabel.textColor"];
  }
    

    
}

@end
